{% extends "base/base.html" %}

{% block title %}上传异常数据 - 项目合作归属数据管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <h2><i class="bi bi-exclamation-triangle"></i> 上传异常数据管理</h2>
        <p class="text-muted">管理批量上传失败的数据记录，支持查看详情和标记处理状态</p>
    </div>
</div>

<!-- 搜索表单 -->
<div class="search-form">
    <form method="GET" class="row g-3">
        <div class="col-md-4">
            <label class="form-label">处理状态</label>
            <select class="form-select" name="status">
                <option value="">全部状态</option>
                <option value="待处理" {% if status_filter == '待处理' %}selected{% endif %}>待处理</option>
                <option value="已处理" {% if status_filter == '已处理' %}selected{% endif %}>已处理</option>
            </select>
        </div>
        <div class="col-md-8 d-flex align-items-end">
            <button type="submit" class="btn btn-primary me-2">
                <i class="bi bi-search"></i> 筛选
            </button>
            <a href="{{ url_for('negotiation.upload_exceptions') }}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-clockwise"></i> 重置
            </a>
        </div>
    </form>
</div>

<!-- 操作工具栏 -->
<div class="row mb-3">
    <div class="col-12">
        <form method="POST" action="{{ url_for('negotiation.mark_exception_processed') }}" id="batchProcessForm">
            <button type="submit" class="btn btn-success" id="batchProcessBtn" disabled>
                <i class="bi bi-check-circle"></i> 改为已处理状态
            </button>
        </form>
    </div>
</div>

<!-- 异常记录列表 -->
<div class="card">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th width="40">
                            <input type="checkbox" class="form-check-input" onchange="toggleSelectAll(this)">
                        </th>
                        <th>文件名</th>
                        <th>处理状态</th>
                        <th>上传人</th>
                        <th>处理人</th>
                        <th>上传时间</th>
                        <th>处理时间</th>
                        <th>异常原因</th>
                        <th width="120">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for exception in exceptions.items %}
                    <tr>
                        <td>
                            <input type="checkbox" class="form-check-input exception-checkbox" 
                                   name="exception_ids" value="{{ exception.id }}" 
                                   form="batchProcessForm" onchange="updateBatchProcessBtn()">
                        </td>
                        <td>{{ exception.filename }}</td>
                        <td>
                            {% if exception.status == '待处理' %}
                                <span class="badge bg-warning">待处理</span>
                            {% else %}
                                <span class="badge bg-success">已处理</span>
                            {% endif %}
                        </td>
                        <td>{{ exception.uploader or '-' }}</td>
                        <td>{{ exception.processor or '-' }}</td>
                        <td>{{ exception.upload_time.strftime('%Y-%m-%d %H:%M:%S') if exception.upload_time else '-' }}</td>
                        <td>{{ exception.process_time.strftime('%Y-%m-%d %H:%M:%S') if exception.process_time else '-' }}</td>
                        <td>
                            <span class="text-truncate" style="max-width: 200px; display: inline-block;" 
                                  title="{{ exception.error_reason }}">
                                {{ exception.error_reason or '-' }}
                            </span>
                        </td>
                        <td>
                            <a href="{{ url_for('negotiation.view_exception_data', exception_id=exception.id) }}" 
                               class="btn btn-outline-info btn-sm" target="_blank">
                                <i class="bi bi-eye"></i> 查看异常数据
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="9" class="text-center text-muted py-4">
                            暂无异常记录
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- 分页 -->
{% if exceptions.pages > 1 %}
<nav class="mt-4">
    <ul class="pagination justify-content-center">
        {% if exceptions.has_prev %}
        <li class="page-item">
            <a class="page-link" href="{{ url_for('negotiation.upload_exceptions', page=exceptions.prev_num, status=status_filter) }}">上一页</a>
        </li>
        {% endif %}
        
        {% for page_num in exceptions.iter_pages() %}
            {% if page_num %}
                {% if page_num != exceptions.page %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('negotiation.upload_exceptions', page=page_num, status=status_filter) }}">{{ page_num }}</a>
                </li>
                {% else %}
                <li class="page-item active">
                    <span class="page-link">{{ page_num }}</span>
                </li>
                {% endif %}
            {% else %}
                <li class="page-item disabled">
                    <span class="page-link">…</span>
                </li>
            {% endif %}
        {% endfor %}
        
        {% if exceptions.has_next %}
        <li class="page-item">
            <a class="page-link" href="{{ url_for('negotiation.upload_exceptions', page=exceptions.next_num, status=status_filter) }}">下一页</a>
        </li>
        {% endif %}
    </ul>
</nav>
{% endif %}

<!-- 统计信息 -->
{% if exceptions.total > 0 %}
<div class="row mt-4">
    <div class="col-lg-6 col-md-12 mb-3">
        <div class="card bg-warning text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4>{{ exceptions.total }}</h4>
                        <p class="mb-0">异常记录总数</p>
                    </div>
                    <div class="align-self-center">
                        <i class="bi bi-exclamation-triangle-fill" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-6 col-md-12 mb-3">
        <div class="card bg-info text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4>{{ exceptions.items | selectattr("status", "equalto", "待处理") | list | length }}</h4>
                        <p class="mb-0">待处理记录</p>
                    </div>
                    <div class="align-self-center">
                        <i class="bi bi-clock-fill" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endif %}
{% endblock %}

{% block extra_js %}
<script>
function toggleSelectAll(checkbox) {
    const checkboxes = document.querySelectorAll('.exception-checkbox');
    checkboxes.forEach(cb => {
        cb.checked = checkbox.checked;
    });
    updateBatchProcessBtn();
}

function updateBatchProcessBtn() {
    const checkedBoxes = document.querySelectorAll('.exception-checkbox:checked');
    const batchBtn = document.getElementById('batchProcessBtn');
    batchBtn.disabled = checkedBoxes.length === 0;
}

// 确认批量处理
document.getElementById('batchProcessForm').addEventListener('submit', function(e) {
    const checkedBoxes = document.querySelectorAll('.exception-checkbox:checked');
    if (checkedBoxes.length === 0) {
        e.preventDefault();
        alert('请选择要处理的异常记录');
        return;
    }
    
    if (!confirm(`确定要将选中的 ${checkedBoxes.length} 条记录标记为已处理吗？`)) {
        e.preventDefault();
    }
});
</script>
{% endblock %}
